<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="editLoginForm" :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:">
                <el-input v-model="formData.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userinfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <jobinfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDataByID } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import userinfo from './components/user-info.vue'
import jobinfo from './components/job-info.vue'
export default {
  components: {
    userinfo, jobinfo
  },
  data() {
    return {
      formData: {
        username: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 一进来就加载数据
    this.getDataByID()
  },
  methods: {
    // 根据id获取点击的用户信息
    async getDataByID() {
      // 根据id获取点击的用户信息
      await getDataByID(this.$route.params.id)
      // 数据回显
      this.formData = await getDataByID(this.$route.params.id)
    },
    // 修改用户名和密码
    async submit() {
      // 表单验证
      await this.$refs.editLoginForm.validate()
      // 发送请求
      await saveUserDetailById({ ...this.formData, password: this.formData.password2 })
      // 成功提示
      this.$message.success('修改成功')
      // 更新页面
      this.getDataByID()
    }
  }
}
</script>

<style>

</style>
